/**********************************************************************
 ** Copyright (C) 2009 Nokia Corporation.
 ** All rights reserved.
 **
 ** $LICENSE_TEXT$
 **********************************************************************/

#include "btsingledeviceselector.h"

#include <QDBusInterface>
#include <QDBusObjectPath>
#include <QtCore/QStringList>
#include <conbtdialogs-dbus.h>

#include <QDebug>

BtSingleDeviceSelector::BtSingleDeviceSelector(QObject *parent) :
    QObject(parent)
{
}

void BtSingleDeviceSelector::searchResult(const QDBusMessage& message)
{
    QList<QVariant> result = message.arguments();
    QString address = result.at(0).toString();
    QString name = result.at(1).toString();
    QString icon = result.at(2).toString();
    QString majorClass = result.at(3).toString();
    QString minorClass = result.at(4).toString();
    bool isTrusted = result.at(5).toBool();
    QStringList services = result.at(6).toStringList();

    qDebug() << address;
    qDebug() << name;
    qDebug() << icon;
    qDebug() << majorClass;
    qDebug() << minorClass;
    qDebug() << isTrusted;
    qDebug() << services.join(" ");
    qDebug() << "**************************";

    emit deviceSelected(address);
}

void BtSingleDeviceSelector::show()
{
    QDBusConnection systemBus = QDBusConnection::systemBus();

    QDBusInterface icdInterface(CONBTDIALOGS_DBUS_SERVICE, CONBTDIALOGS_DBUS_PATH,
            CONBTDIALOGS_DBUS_INTERFACE, systemBus, this);

    if (!icdInterface.isValid()) {
        emit deviceSelected(QString());
        return;
    }

    systemBus.connect(CONBTDIALOGS_DBUS_SERVICE, CONBTDIALOGS_DBUS_PATH,
            CONBTDIALOGS_DBUS_INTERFACE, CONBTDIALOGS_SEARCH_SIG, this,
            SLOT(searchResult(const QDBusMessage&)));

    icdInterface.call(CONBTDIALOGS_SEARCH_REQ, QString(""), QString(""), QStringList(), QString(""));
}
